// pages/tianjiadizhi/tianjiadizhi.js
const app=getApp();
//获得全局js变量
var util = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id:0,
        applyinfo:[],
        xxaddress_: '', 
        address_: '', 
        businessName_: '', 
        cname_: '', 
        otherPhone_: '', 
        name_: '',
        phone_: '',
        province_: '', 
        provincecode_: '', 
        city_: '', 
        citycode_: '', 
        area_: "", 
        areacode_:'',
        imgurl:app.data.imgurl,
        region: ["请选择地址",'',''],
        btnable: true,
    },
    //输入框监听
    bindinput: function (e) {
        //console.log(e);
        var value = e.detail.value;
        var type = e.currentTarget.dataset.type;
        if (type.length > 0) {
            switch (type) {
                case 'name': //姓名
                    this.setData({
                        name_: value,
                    })
                    break;
                case 'address': //地址
                    this.setData({
                        address_: value,
                    })
                    break;
                case 'phone': //手机号码
                    this.setData({
                        phone_: value,
                    })
                    break;
                case 'otherPhone': //手机号码
                    this.setData({
                        otherPhone_: value,
                    })
                    break;
                case 'address': //手机号码
                    this.setData({
                        address_: value,
                    })
                    break;
                case 'businessName': //手机号码
                    this.setData({
                        businessName_: value,
                    })
                    break;
                case 'cname': //手机号码
                    this.setData({
                        cname_: value,
                    })
                    break;
            }
        }
        //console.log(this.data);
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getaddress();
    },
    getaddress:function(){
        var that = this
        var userid=app.globalData.userId;
        util.httpRrquest(app.data.rooturl + 'user/getUserFamilyApply', 'GET', {})
            .then(function (response) {
                that.setData({
                    applyinfo: response.data.data
                });
                var addinfo = response.data.data;
                if(addinfo.hasData == 1){
                    that.setData({
                        id:addinfo.id,
                        xxaddress_: addinfo.address,
                        name_: addinfo.name,
                        phone_: addinfo.phone,
                        province_: addinfo.province, 
                        provincecode_: addinfo.provincecode,
                        city_: addinfo.city, 
                        citycode_: addinfo.citycode,
                        area_: addinfo.area, 
                        areacode_: addinfo.areacode,
                        region: [addinfo.province, addinfo.city, addinfo.area],
                        otherPhone_: addinfo.otherPhone,
                        address_: addinfo.address,
                        businessName_: addinfo.businessName,
                        cname_: addinfo.cname
                    });
                }
            }, function (error) {
                console.log(error);
            });
    },
    addeditaddress:function(){
        var that = this
        if ( that.data.name_.length <= 0 || that.data.phone_.length <= 0 || that.data.province_.length <= 0 || that.data.city_.length <= 0 || that.data.area_.length <= 0 || that.data.address_.length <= 0 || that.data.otherPhone_.length <= 0 || that.data.cname_.length <= 0 || that.data.businessName_.length <= 0) {
            wx.showToast({
                title: '请正确输入信息',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        if (!that.data.btnable) {
            return;
        }
        this.setData({
            btnable: false,
        })
        var userid=app.globalData.userId;
        var postdata = {
            userid: userid,
            id: that.data.id,
            xxaddress: that.data.xxaddress_,
            name: that.data.name_,
            phone: that.data.phone_,
            province: that.data.province_, provincecode: that.data.provincecode_,
            city: that.data.city_, citycode: that.data.citycode_,
            area: that.data.area_, areacode: that.data.areacode_,
            address: that.data.address_,
            otherPhone: that.data.otherPhone_,
            businessName: that.data.businessName_,
            cname: that.data.cname_
        }
        util.httpRrquest(app.data.rooturl + 'user/addFamilyApplyInfo', 'POST', postdata)
            .then(function (response) {
                wx.showToast({
                    title: response.data.msg,
                    icon: 'none',
                    duration: 2000
                })
                setTimeout(function () {
                    wx.navigateBack({
                        delta: 1
                    })
                }, 2000);
            }, function (error) {
                console.log(error);
            });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        var shareinfo = app.setShareMessage();
        console.log(shareinfo);
        return {
            title: shareinfo.title,
            path: shareinfo.path,
            imageUrl: shareinfo.imageUrl,
            success: (res) => {
                wx.showToast({
                    title: "分享成功",
                    icon: 'loading'
                })
            }
        }
    },
    bindRegionChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        console.log('picker发送选择改变，携带值为', e.detail.code)
        console.log('picker发送选择改变，携带值为', e.detail.postcode)
        var value = e.detail.value; 
        var code = e.detail.code; 
        var postcode = e.detail.postcode;
        this.setData({
            region: e.detail.value,
            province_: value[0],
            provincecode_: code[0],
            city_: value[1],
            citycode_: code[1],
            area_: value[2],
            areacode_: code[2],
        })
    }
})